package com.fulihui.weixinsdk.request.material;

import com.alibaba.fastjson.JSON;
import com.fulihui.weixinsdk.http.HttpMethodEnum;
import com.fulihui.weixinsdk.request.WeixinRequest;
import com.fulihui.weixinsdk.result.material.MaterialGetResult;
import com.google.gson.Gson;
import lombok.Data;

import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

/**  获取素材列表
 * Created by hongwang on 2018/1/9.
 */
@Data
public class MaterialGetReuest extends WeixinRequest<MaterialGetResult> {

    private transient String  access_token;

     /***
     * 素材的类型，图片（image）、视频（video）、语音 （voice）、图文（news）
     */
    private String type;

    /**
     * 从全部素材的该偏移位置开始返回，0表示从第一个素材 返回
     */
    private  Integer offset;

    /**
     * 返回素材的数量，取值在1到20之间
     */
    private  Integer count;


    @Override
    public Map<String, String> urlParam() {
        Map<String,String> param=new TreeMap<String,String>();
        if(!Objects.isNull(access_token)){
            param.put("access_token",access_token);
        }
        return param;
    }

    @Override
    protected void childParam() {

    }

    @Override
    public String service() {
        return "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
    }

    @Override
    public String urlEndStr() {
        return null;
    }

    @Override
    public MaterialGetResult parseResult(String respStr) {
        return new Gson().fromJson(respStr,MaterialGetResult.class);
    }

    @Override
    public HttpMethodEnum httpMethod() {
        return HttpMethodEnum.POST;
    }

    @Override
    public String requestData() {
        return JSON.toJSONString(this);
    }


    public static void main(String[] args) {
        String respStr = "{\n" +
                "  \"item\": [\n" +
                "    {\n" +
                "      \"media_id\": \"O7xsNHohKG1N4BYgnHrMh9lBszLg9D1MGcAncrlJHqU\",\n" +
                "      \"content\": {\n" +
                "        \"news_item\": [\n" +
                "          {\n" +
                "            \"title\": \"素材2\",\n" +
                "            \"author\": \"young\",\n" +
                "            \"digest\": \"内容2\",\n" +
                "            \"content\": \"<p>内容2</p><p><img data-s=\\\"300,640\\\" data-type=\\\"jpeg\\\" data-src=\\\"https://mmbiz.qpic.cn/mmbiz_jpg/75UZPx3sTRXzw4mUQJPqDF9tT3B3kl5IB5eUiaPnPtiaSXqoxYOxd7wpjxu42CN6IpmzkTgN75Dj8IZMvo4DxJ5Q/0?wx_fmt=jpeg\\\" data-copyright=\\\"0\\\" style=\\\"\\\" class=\\\"\\\" data-ratio=\\\"0.8294871794871795\\\" data-w=\\\"780\\\"  /></p><p><br  /></p>\",\n" +
                "            \"content_source_url\": \"\",\n" +
                "            \"thumb_media_id\": \"O7xsNHohKG1N4BYgnHrMh8YJVRxrmABDS2euLFB_9sU\",\n" +
                "            \"show_cover_pic\": 0,\n" +
                "            \"url\": \"http://mp.weixin.qq.com/s?__biz=MzU4OTIzMTI3Mg==&mid=100000010&idx=1&sn=d2d2796a7f7557086a1a26e05f30e637&chksm=7dd1e4cd4aa66ddbd68835cb0ade4cbaaa7b003a4f46a0b433e8db47055425b353bb0429c946#rd\",\n" +
                "            \"thumb_url\": \"http://mmbiz.qpic.cn/mmbiz_jpg/75UZPx3sTRXzw4mUQJPqDF9tT3B3kl5IsMNlkR3UMYoicukYiaBibscu4zX1G9F2qJPK1HMT3RU21LjoribIWkXByQ/0?wx_fmt=jpeg\",\n" +
                "            \"need_open_comment\": 1,\n" +
                "            \"only_fans_can_comment\": 0\n" +
                "          }\n" +
                "        ],\n" +
                "        \"create_time\": 1515121046,\n" +
                "        \"update_time\": 1515121051\n" +
                "      },\n" +
                "      \"update_time\": 1515121051\n" +
                "    },\n" +
                "    {\n" +
                "      \"media_id\": \"O7xsNHohKG1N4BYgnHrMhy92OWPDioSLHUKKtMXDfyU\",\n" +
                "      \"content\": {\n" +
                "        \"news_item\": [\n" +
                "          {\n" +
                "            \"title\": \"测试分组推文\",\n" +
                "            \"author\": \"abc\",\n" +
                "            \"digest\": \"abcd\",\n" +
                "            \"content\": \"<p>测试<br  /></p>\",\n" +
                "            \"content_source_url\": \"\",\n" +
                "            \"thumb_media_id\": \"O7xsNHohKG1N4BYgnHrMh4Lwne_PYu3APclTJHrxzEI\",\n" +
                "            \"show_cover_pic\": 0,\n" +
                "            \"url\": \"http://mp.weixin.qq.com/s?__biz=MzU4OTIzMTI3Mg==&mid=100000003&idx=1&sn=bda1c69b6fa580ce1ee63ab24c535efe&chksm=7dd1e4c44aa66dd2016328ffed94c456b68aa558843af4e726856d8d5a114687ac805a689e00#rd\",\n" +
                "            \"thumb_url\": \"http://mmbiz.qpic.cn/mmbiz_jpg/75UZPx3sTRVXRJJDPnmXG1EAhKibsor6qutnQg9P0fUJaggl64mP26L5TJZSYiaScia7bQAyDOYg9goE3MYSCicatA/0?wx_fmt=jpeg\",\n" +
                "            \"need_open_comment\": 0,\n" +
                "            \"only_fans_can_comment\": 0\n" +
                "          },\n" +
                "          {\n" +
                "            \"title\": \"title2\",\n" +
                "            \"author\": \"young\",\n" +
                "            \"digest\": \"test title2\",\n" +
                "            \"content\": \"<p>test title2</p><p><img data-s=\\\"300,640\\\" data-type=\\\"jpeg\\\" data-src=\\\"https://mmbiz.qpic.cn/mmbiz_jpg/75UZPx3sTRXzw4mUQJPqDF9tT3B3kl5It6KiaPF7LPflVNsmpf4RCIluXUIIib8B8R5v8eQoNDXlFoB8Y4ASmWOQ/0?wx_fmt=jpeg\\\" data-copyright=\\\"0\\\" style=\\\"\\\" class=\\\"\\\" data-ratio=\\\"1.2878504672897195\\\" data-w=\\\"535\\\"  /></p><p><br  /></p>\",\n" +
                "            \"content_source_url\": \"\",\n" +
                "            \"thumb_media_id\": \"O7xsNHohKG1N4BYgnHrMh0DRz4kf8yKQvFaJM1NRgjI\",\n" +
                "            \"show_cover_pic\": 0,\n" +
                "            \"url\": \"http://mp.weixin.qq.com/s?__biz=MzU4OTIzMTI3Mg==&mid=100000003&idx=2&sn=5c82b69feebb5df6d0849cc1544339f0&chksm=7dd1e4c44aa66dd21c24d741cf31ce3491eddf6d27ac7623b52b07b18e85a33ff466ce994200#rd\",\n" +
                "            \"thumb_url\": \"http://mmbiz.qpic.cn/mmbiz_jpg/75UZPx3sTRXzw4mUQJPqDF9tT3B3kl5IdAg2cjfIrdWhmI3G9zwRHgBo4ibmPWEXfwhib7thdHLibPsxglGSs6Qcg/0?wx_fmt=jpeg\",\n" +
                "            \"need_open_comment\": 1,\n" +
                "            \"only_fans_can_comment\": 0\n" +
                "          }\n" +
                "        ],\n" +
                "        \"create_time\": 1508400472,\n" +
                "        \"update_time\": 1515119761\n" +
                "      },\n" +
                "      \"update_time\": 1515119761\n" +
                "    }\n" +
                "  ],\n" +
                "  \"total_count\": 2,\n" +
                "  \"item_count\": 2\n" +
                "}";

        MaterialGetResult materialGetResult = new Gson().fromJson(respStr, MaterialGetResult.class);
        System.out.println(materialGetResult);
    }
}
